/* Copyright (c) 2017 meigsmart, Inc.  All Rights Reserved.
* meigsmart Proprietary and Confidential.
*
*
*File name : fw_interface.h
*Date :2017-5-15
*Abstract :All declared in this header file are interface definitions for external use by the customer
*
*
*The current authors : lileilei
*Completion date :2023-09-14
*
*/

 
#ifndef __FW_INTERFACE_H__
#define __FW_INTERFACE_H__
#include <stdio.h>

#ifdef  __cplusplus
extern "C" {
#endif
  
#include "misc/misc_api.h"

int fw_lib_ready(void);/*return value : 0 . all OK ; value :1 . sim card not ready; value :-1 .modem not ready*/
int fw_lib_init(void);

typedef struct
{
	int gpio7;
    int gpio10;
    int gpio14;
}fw_hw_ver_s_type;

int fw_ver_get_pcba(fw_hw_ver_s_type *hwver);
int fw_ver_get_fw(struct FW_VER_STRU *pVerStru);
int fw_ver_get_lib(struct FW_VER_STRU *pVerStru);

int fw_modem_check(void);
int fw_rtc_alarm(int nSecond);
void fw_wdt_feed(void);
int fw_system_reboot(void);
int fw_shutdown_now(void);
int fw_sw_test_set_nv_flag(void);
int fw_modem_wake_up_ble(void);

typedef struct
{
	char uart_path[32];  /*eg: /dev/ttyHSL1*/
	unsigned int wait_time;       /* delay time 0-50: 0-> no wait, 1-20s-> wait 100ms- 2s*/
	int baud;            /* baud rate : 9600, 57600, 115200, 230400, 460800, 500000, 576000 */
}fw_uart_dev_s_t;
int fw_set_uart_init(fw_uart_dev_s_t dev, int *fd_uart);
typedef enum
{
    FW_LOG_LEVEL_ASSET = 0,
    FW_LOG_LEVEL_ERROR,
    FW_LOG_LEVEL_WARN,
    FW_LOG_LEVEL_INFO,
    FW_LOG_LEVEL_DEBUG
}FW_LOG_LEVEL;

typedef void(*FW_LogCallback)(unsigned char , char*);
int fw_sdk_log_register(FW_LogCallback callbackfucn);


struct FW_BAT_INFO_STRU
{
	float temp;
	float volt;
	float power;   /*percentage of total power*/	
};
int fw_bat_get_status(struct FW_BAT_INFO_STRU *info);

typedef enum
{
    FW_MAIN_POWER, /* main power used */
    FW_BAT_POWER,  /* backup bat used */
    FW_POWER_MAX
}fw_power_stat_e_type;
int fw_get_supply_power_status(fw_power_stat_e_type * status);

int fw_sound_check(void);
/*    sound format :1 for pcm; 2 for wav *//*file <= 25 bool*/
int fw_sound_play(int format,char* file);  /*if success, return 0 , if fail, return -1*/
int fw_sound_stop(void);
int fw_sound_adj_volume(int volume);/*  10 - 175*/

//Voice broadcast state
typedef enum{
	FW_APLAY_STATUS_MIN = 0,
	FW_APLAY_STATUS_STOP = FW_APLAY_STATUS_MIN,//Play to complete
	FW_APLAY_STATUS_RUNNING = 1,//In the play
	FW_APLAY_STATUS_ERROR = 2,//Play failed
	FW_APLAY_STATUS_MAX
}fw_aplay_status_e;

/**
	Function:fw_sound_play_get_status <To track the status of speech>
	Parameter:int *status <Use for storage voice broadcast state>
	Return:if success, return 0 , if fail, return -1
*/
int fw_sound_play_get_status(int *status);

typedef enum
{
    GPS_ON_POSITIONING,
    GPS_IS_FIXED,
    GPS_POWER_CLOSED,
    GPS_STATE_MAX
}gps_locate_status_e_type;

struct FW_GPS_INFO_STRU
{
	int azimuth;     /*0-359*/
	float speed;     /*0.0-999.9 km/h */
	float altitude;  /*xxxxx.xm*/
	double longitude; /*xxx.xxxxxx*/
	double latitude;  /*xx.xxxxxx*/
	char utc_time[32];  /*yyyymmddhhmmss*/
	float horizontal_accuracy; /*0-50 m*/
	float vertical_accuracy;/*0-50 m*/
	int satellites_num ; /*0-12*/
    gps_locate_status_e_type status;
    int cn0[12]; /* state cn0 num max ->12, actual dependon on "satellites_num" */
};
int fw_gps_check(void);
int fw_gps_get_info(struct FW_GPS_INFO_STRU *info);
int fw_gps_up_alp(void);
struct FW_GPS_SOURCE_INFO
{
	char source_data[1024];
};
int fw_get_gps_source_info(struct FW_GPS_SOURCE_INFO *info);


int fw_gsensor_check(void);
int fw_gyroscope_check(void);
struct FW_GSENSOR_INFO_STRU
{
	float acc_x;
	float acc_y;
	float acc_z;
	float temp;   /*temperature*/
};
struct FW_GYROSCOPE_INFO_STRU
{
	float gyro_x;
	float gyro_y;
	float gyro_z;
};
int fw_gsensor_get_info(struct FW_GSENSOR_INFO_STRU *info);
int fw_gsensor_set_threshold(int nduration); //0-255
int fw_gyroscope_get_info(struct FW_GYROSCOPE_INFO_STRU *info);
  /* frequency  1 -12  ----default  8 ->100HZ.  only set   8-10.
  datasheet 上说明8-10。5以下会导致设置失效而导致中断不停上报
  	 5 -> 25/2 Hz ;6 -> 25Hz; 7->50 Hz; 8 -> 100Hz;
	9 -200HZ ;10 -- 400Hz;  11 ->800Hz;12->1600Hz;
  */
int fw_gsensor_set_sampled_frequency(int frequency); //frequency  8-10;
int fw_gsensor_get_sampled_frequency(int *frequency);   

/*gsensor measure range : 2--2G;  4--4G  ;  8--8G;  16--16G*/
int fw_gsensor_set_measure_range(int nRange) ;  /* nRange  only 2  ,4   , 8  ,16 */
int fw_gsensor_get_measure_range(int *nRange) ;  /* G*/

/*gyroscope measure range : 0--2000 o/s;  1--1000 o/s  ;  2--500 o/s;  3--250 o/s ,4 -- 125 o/s */
 int fw_gyroscope_set_measure_range(int nRange) ; 
 int fw_gyroscope_get_measure_range(int *nRange) ;
   /* frequency  1 -12  ----default  8 ->100HZ.  only set   8-10.
  datasheet 上说明8-10。
  	 5 -> 25/2 Hz ;6 -> 25Hz; 7->50 Hz; 8 -> 100Hz;
	9 -200HZ ;10 -- 400Hz;  11 ->800Hz;12->1600Hz;
  */
int fw_gyroscope_set_sampled_frequency(int frequency); //frequency  8-10;
int fw_gyroscope_get_sampled_frequency(int *frequency);

typedef struct
{
	char oa[32]; //106550010646
	char dt[32]; //17/09/12,18:58:35+32
	char data[512];
}FW_SMS_info;

int fw_network_get_sms_index_available(char* indexfile);
int fw_network_get_index_sms(int index, FW_SMS_info* msg, int* bEnable);//bEnable 返回0 index OK, 返回-1  index 无此下标. 
int fw_network_delete_index_sms(int index);
/*设置短信网关
	msgnum:  eg. 1.首先看未生效时的设置："+8613800210500",161  ；
前面有个+号，就默认为是国际号码，无论后面设置什么数字，代码都会强制设置为145；

2.正确的设置："8613800210500",161 ；
非国际号码设置时请去掉+号；
	
	msgsca:
	129 ISDN/电话编号方式设计，国家的/世界的未知。
	145 ISDN/电话编号方式设计，世界的号码。
	161 ISDN/电话编号方式设计，国家的号码。
	128~255 

*/
int fw_network_set_ismg(char* msgnum,int msgsca);//msgnum must 13 个号码。
int fw_network_get_ismg(char* msgnum,int *msgsca);

typedef enum
{
    /*0*/
    FW_WAKEUP_EVENT_MIN,

    /*1*/
	FW_WAKEUP_NETWORK_DROP,

    /*2*/
	FW_WAKEUP_NETWORK_STD_SW, /*  2/3/4G switch
							   * +---------+-----------+
							   * | new type|padding... |
							   * +---------+-----------+
							   * new type: after switch, the currenty network type(1,2,3), 1byte.
							   * padding: fill out the left buffer with zero.
							   */

    /*3*/
	FW_WAKEUP_NETWORK_CSQLOW, /*CSQ threshold available after reboot*/

    /*4*/
	FW_WAKEUP_NEW_SMS,

    /*5*/
	FW_WAKEUP_RTC_ALARM,

    /*6*/
	FW_WAKEUP_GSENSOR_EVENT,  /*anymotion  dataArray[0]  0x03; step_count_mode   dataArray[0]  0x05;  */

    /*7*/
    FW_WAKEUP_NETWORK_DISCON,  /*mobile data flow is broken*/

    /*8*/
    FW_WAKEUP_NETWORK_CON,  /*mobile data connected internect*/

    /*9*/
    FW_WAKEUP_NETWORK_REG, 

    /*10*/
    FW_WAKEUP_WDT_EVENT, /*喂狗echo 1 > /sys/devices/hw_watchdog/feed_dog*/

    /*11*/
    FW_WAKEUP_S_BAT_USED_EVENT, /* 0 -> main power used; 1 -> main power closed ,bat used*/

    /*12*/
    FW_WAKEUP_BAT_LOW_POWER_EVENT, /* BAT power less than 3.45V, and interval 300s*/

    FW_WAKEUP_EVENT_MAX
 }FW_WAKEUP_EVENT;

typedef struct
{
	FW_WAKEUP_EVENT event;
	unsigned char dataArray[128];
}FW_WAKEUP_MSG;

typedef struct FW_HANDLER_STRU
{
  FW_WAKEUP_EVENT cmd_id;
  int (*proc_func)(FW_WAKEUP_MSG *pWakeUpApMSG);
}FW_HANDLER_TYPE;
int fw_reg_event_handler(FW_WAKEUP_EVENT msgID,  int (*proc_func)(FW_WAKEUP_MSG *wakeUpApMSG));

struct FW_NETWORK_INFO_STRU
{
	char iccid[32];//always 20 digit
	char imei[24];//always 15 digit
	char mcc[4];     /*mobile country code, always 5 digit*/ 
	char mnc[4];     /*mobile network code, always 5 digit*/
	char cellid[8];  //0~65535
	char lac_id[8];
	int csq_rssi;
	int standard; /*0 not register;1 2g;2 3g;3 4g*/
	int snr;/*only LTE have snr*/
	int rsrp;/*only LTE have rsrp*/
    char sn[20];
};
int fw_network_get_info(struct FW_NETWORK_INFO_STRU* info);

/**
	Function:fw_network_dialon <dial>
	Parameter:int *err_num <qmi_error_type_v01 enum>
	Return:if success, return 0 , if fail, return -1
*/
int fw_network_dialon(int *err_num);
int fw_network_dialoff(void);
int fw_get_network_server_status(int * status); /*status == 1  registered ; status ==0 not register*/
int fw_get_airplane_status(int *status); /*status : 0 - airplane on, 1 - airplane off*/
int fw_network_set_airplane_on(void);
int fw_network_set_airplane_off(void);

int fw_network_get_datastatus(int *status);
/*Set 2/3/4G search order   (now only can set ,1 ,2 ,4, 5,7)
	1	WCDMA ONLY，只注册WCDMA网络
	2	Auto模式，顺序：LTE-TD-SCDMA/WCDMA/EVDO-GSM/CDMA
	3	GSM ONLY，只注册GSM网络
	4	3G PREFERRED 3G优先
	5	LTE ONLY，只注册到LTE网络
	6	TD-SCDMA ONLY，只注册TD-SCDMA网络
	7	TD-SCDMA and WCDMA
	8	CDMA ONLY
	9	CDMA&EVDO
	10	EVDO ONLY                
*/
int fw_network_set_std_order(int mode);
int fw_network_get_std_order(int *mode);

struct FW_APN_CFG_STRU
{
  char apn[32];
  char username[32];
  char password[32];
  int  auth; //鉴权模式0  null;1  PAP; 2  CHAP ; 3 both PAP and  CHAP;

};

/*设置apn*/
int fw_network_set_apn_cfg(struct FW_APN_CFG_STRU *cfg); 
int fw_network_get_apn_cfg(struct FW_APN_CFG_STRU *cfg);
typedef struct fw_netsysinfo_stru
{
    /*0 no services;1 only CS services; 2 only ps srvers; 3 PS+CS services; 4 Find the right network,not register
network*/
    int srv_domain;
    int sim_state;  /*1:UIM card state valid; 244 :ROM SIM;  255:UIM crad ont exist;*/
}fw_netsysinfo_stru_s_tpye;

int fw_network_get_sysinfo(fw_netsysinfo_stru_s_tpye *info);

typedef enum
{
	LOT_MOD_MIN,
	LOT_GSM_MODE,    /*2G*/
	LOT_WCDMA_MODE,  /*3G*/
	LOT_LTE_MODE,    /*4G*/
	LOT_MODE_MAX
}fw_cell_lot_e_type;

typedef struct{
	char mcc[4];	 /*mobile country code, always 5 digit*/
	char mnc[4];	 /*mobile network code, always 5 digit*/
	int cellid;  /*0~65535	*/
	int csq_rssi;
    fw_cell_lot_e_type status; /* cell LOT status */
}fw_cell_s_type;

typedef struct{
	fw_cell_s_type cg_info[20];
    int size;
}fw_cellinfo_s_type;

/**
	函数:fw_get_cellinfo<获取相邻小区信息>
	参数:
		@ fw_cellinfo_s_type *cellinfo 存放相邻小区信息
		@ int cellinfo->size 搜索小区数量
	返回值:请求成功返回搜索到的小区个数
		0 表示查询结果为0，-1表示失败。
**/
int fw_get_cellinfo(fw_cellinfo_s_type *cellinfo);



typedef enum// GPS ,BULTTOOTH,GSENSOR,CODEC can switch
{
	FW_MOD_MIN,
	FW_MOD_GPS,
	FW_MOD_BLUETOOTH,
	FW_MOD_CODEC_PA,	  /*每次播放声音前需要打开PA*/
	FW_MOD_MAX
}FW_MOD;
int fw_onoff_mod_pwr(FW_MOD module,int flag);//module  only GPS ,GPS_VBAK ,BULTTOOTH, GSENSOR, CODEC can switch

int fw_set_enable_usbport(int en_status); /*  en_status : 0 - disable, 1 - enable*/
typedef enum
{
    USB_UNLOCK,
    USB_LOCK,
    USB_MAX
}fw_usb_e_type;
int fw_get_usbport_lock_status(fw_usb_e_type *status);
int fw_sleep(void);
int fw_quit_autosleep(void);

typedef struct FW_STRU_AT_CMD
{
   char cmd[1024];
}FW_AT_CMD;

typedef struct FW_STRU_AT_RESULT
{
    char result[1024];
}FW_AT_RESULT;

//At passthrough error code define
typedef enum{
	FW_AT_ERROR_MIN = 0,
	FW_AT_NO_ERROR = FW_AT_ERROR_MIN,
	FW_AT_TIMEOUT,
	FW_AT_WRITE_ERROR,
	FW_AT_READ_ERROR,
	FW_AT_PARA_ERROR,//Parameter error
	FW_AT_OTHER_ERROR,
	FW_AT_ERROR_MAX
}fw_at_error_e;

/**
	Function:fw_at_cop <The AT passthrough>
	Parameter:
		@ FW_AT_CMD *at_cmd          AT command
		@ FW_AT_RESULT *at_result   AT result
		@ unsigned int timeout <range:0~65535/seconds>
	Return:The enumeration of fw_at_error_e
*/
int fw_at_cop(FW_AT_CMD *at_cmd, FW_AT_RESULT *at_result, unsigned int timeout);

typedef enum{
    FW_DISABLE,
    FW_ENABLE,
    FW_EN_MAX
}fw_en_e_type;

typedef enum{
    FW_LED_MIN,
    FW_LED_BLUE,
    FW_LED_RED,
    FW_LED_GREEN,
    FW_LED_MAX
}fw_led_e_type;
int fw_ctl_enable_led(fw_led_e_type ctl_led, fw_en_e_type status);
int fw_set_enable_ecu_power(fw_en_e_type status);
int fw_set_enable_mcu_power(fw_en_e_type status);
int fw_update_slcan_function(char * farmware_path);

typedef struct
{
   int pressure;  /* unit: pa */
   float temperature;  /* unit: Celsius degree */
}fw_barometer_s_tpye;
int fw_get_barometer(fw_barometer_s_tpye *pressure);

typedef enum
{
    FW_CHARGERING, /* chargering */
    FW_CHARGEREND, /* not chargering */
    FW_CHARGER_MAX
}fw_charger_status_e_tpye;
int fw_bat_charger_state(fw_charger_status_e_tpye *status);
int fw_enable_charger(fw_en_e_type status);

    
#define MAX_WIFI_SCAN_NUM	(64)
#define MIN_WIFI_SCAN_NUM	(0)
#define MAX_WIFI_SCAN_TIME	(60)
#define MIN_WIFI_SCAN_TIME	(0)

typedef struct NIC_INFO_S{
    char mac[32];
}NIC_INFO;//网卡物理信息

typedef enum 
{
    FW_WIFI_AP_OPEN = 0, 
    FW_WIFI_AP_SHARED = 1, 
    FW_WIFI_AP_WPA_PSK = 2, 
    FW_WIFI_AP_WPA2_PSK = 3, 
    FW_WIFI_AP_WPA_WPA2_PSK = 4, 
    FW_WIFI_AP_AUTH_MAX = 5
}FW_WIFI_AUTH_E;

typedef struct AP_S{
        char mac[18]; //such as 94:65:2d:c0:1b:f8
        int rssi;
        int channel;    
        unsigned char ssid[120]; //UTF-8 编码
        FW_WIFI_AUTH_E crypto; 
}AP_INFO;


 typedef struct DEV_INFO_SCAN_S{
    AP_INFO scan_result[MAX_WIFI_SCAN_NUM];
    int ap_num;
 }DEV_INFO_SCAN;


int fw_wifi_open(void);
int fw_wifi_close(void);
int fw_wifi_get_nic_info(NIC_INFO* info);
int fw_wifi_set_max_ap_num(int num);
int fw_wifi_begin_scan(DEV_INFO_SCAN* info); 





#ifdef  __cplusplus
}
#endif

#endif


